﻿// -----------------------------------------------------------------------
// <copyright file="ClientRepository.cs" company="AGR">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AGR.Data.Repository.Contract
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using AGR.Model;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ClientRepository : AbstractRepository<Client>
    {
        public ClientRepository(ContextHelper contextHelper)
        {
            base._contextHelper = contextHelper;
            base._context = _contextHelper.GetContext();
        }

        public override IQueryable<Client> GetAll()
        {
            var result = (from x in base._context.Clients
                          select new Client
                          {
                              Id = x.Id,
                              ClientCode = x.ClientCode,
                              Title = x.Title,
                              Description = x.Description,
                              Email = x.Email,
                              TelNumber = x.TelNumber,
                              FaxNumber = x.FaxNumber,
                              PhysicalAddress = x.PhysicalAddress,
                              PostalAddress = x.PostalAddress,
                              DateCreated = x.DateCreated,
                              UserCreated = x.UserCreated,
                              DateModified = x.DateModified,
                              UserModified = x.UserModified
                          });

            return result;
        }

        public override void Add(Client entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Client entityToAdd = new Context.Client
            {
                Id = entity.Id,
                ClientCode = entity.ClientCode,
                Title = entity.Title,
                Description = entity.Description,
                Email = entity.Email,
                TelNumber = entity.TelNumber,
                FaxNumber = entity.FaxNumber,
                PhysicalAddress = entity.PhysicalAddress,
                PostalAddress = entity.PostalAddress,
                DateCreated = DateTime.Now,
                UserCreated = entity.UserCreated,
                DateModified = null,
                UserModified = null
            };

            base._context.Clients.InsertOnSubmit(entityToAdd);
        }

        public override void Update(Client entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Client entityToUpdate = base._context.Clients.Single(x => x.ClientCode.Equals(entity.ClientCode));

            entityToUpdate.ClientCode = entity.ClientCode;
            entityToUpdate.Title = entity.Title;
            entityToUpdate.Description = entity.Description;
            entityToUpdate.Email = entity.Email;
            entityToUpdate.TelNumber = entity.TelNumber;
            entityToUpdate.FaxNumber = entity.FaxNumber;
            entityToUpdate.PhysicalAddress = entity.PhysicalAddress;
            entityToUpdate.PostalAddress = entity.PostalAddress;
            entityToUpdate.DateModified = DateTime.Now;
            entityToUpdate.UserModified = entity.UserModified;
        }

        public override void Delete(Client entity)
        {
            if (entity == null)
                throw new ArgumentNullException();

            Context.Client entityToDelete = base._context.Clients.Single(x => x.ClientCode.Equals(entity.ClientCode));

            base._context.Clients.DeleteOnSubmit(entityToDelete);
        }
    }
}
